using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.CodeRush.Core;
using DevExpress.CodeRush.StructuralParser;
using CR.StyleCop.Documentation;

namespace CR.StyleCop.Utils {
    public static class SourceUtils {

        public static List<StyleCopIssue> EmptyIssueList = new List<StyleCopIssue>(0);

        public static IEnumerable<T> GetElementEnumerator<T>(IElement element) {
            return SourceModelUtils.GetElementEnumerator(element, new ElementTypeFilter(typeof(T))).Cast<T>();
        }

        public static LanguageElement ConvertToLanguageElement(IElement element) {
            return LanguageElementRestorer.ConvertToLanguageElement(element);
        }

        public static T ConvertToLanguageElement<T>(IElement element) where T : LanguageElement {
            return LanguageElementRestorer.ConvertToLanguageElement(element) as T;
        }

        public static void AddIssue(CheckCodeIssuesEventArgs ea, StyleCopIssue issue) {
            switch (issue.Type) {
                case StyleCopIssueType.Hint:
                    ea.AddHint(issue.Element.FirstNameRange, issue.IssueDescription);
                    break;
                case StyleCopIssueType.DeadCode:
                    ea.AddDeadCode(issue.Element.FirstNameRange, issue.IssueDescription);
                    break;
                case StyleCopIssueType.Error:
                    ea.AddError(issue.Element.FirstNameRange, issue.IssueDescription);
                    break;
                case StyleCopIssueType.Warning:
                    ea.AddWarning(issue.Element.FirstNameRange, issue.IssueDescription);
                    break;
            }
        }

        public static IEnumerable<IElement> GetElementEnumerator<T1, T2, T3, T4, T5, T6, T7, T8, T9>(IElement element) {
            List<IElement> elements = new List<IElement>();
            elements.AddRange(SourceModelUtils.GetElementEnumerator(element, new ElementTypeFilter(typeof(T1))));
            elements.AddRange(SourceModelUtils.GetElementEnumerator(element, new ElementTypeFilter(typeof(T2))));
            elements.AddRange(SourceModelUtils.GetElementEnumerator(element, new ElementTypeFilter(typeof(T3))));
            elements.AddRange(SourceModelUtils.GetElementEnumerator(element, new ElementTypeFilter(typeof(T4))));
            elements.AddRange(SourceModelUtils.GetElementEnumerator(element, new ElementTypeFilter(typeof(T5))));
            elements.AddRange(SourceModelUtils.GetElementEnumerator(element, new ElementTypeFilter(typeof(T6))));
            elements.AddRange(SourceModelUtils.GetElementEnumerator(element, new ElementTypeFilter(typeof(T7))));
            elements.AddRange(SourceModelUtils.GetElementEnumerator(element, new ElementTypeFilter(typeof(T8))));
            elements.AddRange(SourceModelUtils.GetElementEnumerator(element, new ElementTypeFilter(typeof(T9))));
            return elements;
        }

        public static IEnumerable<IElement> GetElementEnumerator<T1, T2>(IElement element) {
            List<IElement> elements = new List<IElement>();
            elements.AddRange(SourceModelUtils.GetElementEnumerator(element, new ElementTypeFilter(typeof(T1))));
            elements.AddRange(SourceModelUtils.GetElementEnumerator(element, new ElementTypeFilter(typeof(T2))));
            return elements;
        }
    }   
}
